package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    OrdersMapper ordersMapper;
//    @Scheduled(cron = "0 * * * * ?")
    public void processOutTimeDelivery(){
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders> list = ordersMapper.getByStatusWithOrderTimeTL(Orders.PENDING_PAYMENT,time);
        if(list != null && list.size() > 0){
            for(Orders orders : list){
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("订单超时，已取消");
                ordersMapper.update(orders);
            }
        }

    }

//    @Scheduled(cron = "0/5 * * * * ?")
    public void processCompleteDelivery(){
        LocalDateTime time = LocalDateTime.now().plusHours(-1);
        List<Orders> list = ordersMapper.getByStatusWithOrderTimeTL(Orders.DELIVERY_IN_PROGRESS,time);
        if(list != null && list.size() > 0){
            for(Orders orders : list){
                orders.setStatus(Orders.COMPLETED);
                ordersMapper.update(orders);
            }
        }
    }
}
